package com.example.yaoalarm;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class selecttime extends AppCompatActivity {

    private TimePicker mTimepicker;
    private Button changetm;
    private int thehour;
    private int themin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selecttime);

        Calendar c = Calendar.getInstance();
        thehour = c.get(Calendar.HOUR_OF_DAY);
        themin = c.get(Calendar.MINUTE);

        mTimepicker= this.findViewById(R.id.ttime);
        changetm = this.findViewById(R.id.stmbtn);
        //设置是否24小时制显示
        mTimepicker.setIs24HourView(true);
        //禁用键盘输入
        mTimepicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
        mTimepicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
//                Toast.makeText(selecttime.this,hourOfDay+"时"+minute+"分",Toast.LENGTH_SHORT).show();
                thehour = hourOfDay;
                themin = minute;
            }
        });
        changetm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                Bundle bundle=new Bundle();
                bundle.putInt("flag", 1);
                bundle.putInt("hour", thehour);
                bundle.putInt("minute", themin);
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        Bundle bundle=new Bundle();
        bundle.putInt("flag", 0);
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        finish();
    }
}